package com.mmall.service.impl;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.common.TokenCache;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.mmall.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by Administrator on 2017/11/1 0001.
 */
@Service("iUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public ServerResponse<User> login(String username, String password) {
        int count = userMapper.checkUsername(username);
        if (count == 0){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String mPassword = MD5Util.MD5EncodeUtf8(password);
        User user = userMapper.selectLogin(username,mPassword);
        if (user == null){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        user.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登录成功",user);
    }

    @Override
    public ServerResponse<String> register(User user) {
        ServerResponse validResponse = this.checkValid(user.getUsername(),Const.USERNAME);
        if (!validResponse.isSuccess()){
            return validResponse;
        }
        validResponse = this.checkValid(user.getEmail(),Const.EMAIL);
        if (!validResponse.isSuccess()){
            return validResponse;
        }
        String newPassword = MD5Util.MD5EncodeUtf8(user.getPassword());
        user.setPassword(newPassword);
        int resultCount = userMapper.insert(user);
        if (resultCount == 0){
            return ServerResponse.createByErrorMessage("注册失败");
        }
        return ServerResponse.createBySuccessMessage("注册成功");
    }

    @Override
    public ServerResponse<String> checkValid(String name, String type) {

        if (StringUtils.isNotBlank(type)){
            if (Const.USERNAME.equals(type)){
                int resultCount = userMapper.checkUsername(name);
                if (resultCount > 0){
                    return ServerResponse.createByErrorMessage("用户名已存在");
                }}
            if (Const.EMAIL.equals(type)){
                int resultCount = userMapper.checkEmail(name);
                if (resultCount > 0){
                    return ServerResponse.createByErrorMessage("邮箱已存在");
                }}
        }else {
            return ServerResponse.createByErrorMessage("参数错误");
        }


        return ServerResponse.createBySuccessMessage("校验成功");
    }


    public ServerResponse<String> selectUsernameQuestion(String username){
        ServerResponse response = this.checkValid(username,Const.USERNAME);
        if (response.isSuccess()){
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        String question = userMapper.selectUserQuestion(username);
        if (StringUtils.isNotBlank(question)){
            return ServerResponse.createBySuccess(question);
        }
        return ServerResponse.createByErrorMessage("用户没有设置问题");


    }


    public ServerResponse<String> checkAnswer(String username,String question,String answer){

        int count = userMapper.checkAnswerUsername(username,question,answer);
        if (count > 0){
            String forgetToken = UUID.randomUUID().toString();
            TokenCache.setKey(TokenCache.TONKEN_PREFIX+username,forgetToken);
            return ServerResponse.createBySuccess(forgetToken);
        }
        return ServerResponse.createByErrorMessage("问题答案错误");



    }


    public ServerResponse<String> restUserPassword(String username,String password,String forgetToken){
        if (StringUtils.isBlank(forgetToken)){
            return ServerResponse.createByErrorMessage("参数错误，token需要传递");
        }
        ServerResponse response = this.checkValid(username,Const.USERNAME);
        if (response.isSuccess()){
            return ServerResponse.createByErrorMessage("用户不存在");
        }
        String token = TokenCache.getKey(TokenCache.TONKEN_PREFIX+username);
        if (StringUtils.isBlank(token)){
            return ServerResponse.createByErrorMessage("token无线或已过期");
        }
        if (StringUtils.equals(token,username)){
            String passwordNew = MD5Util.MD5EncodeUtf8(password);
            int count = userMapper.updateUserPassword(username,passwordNew);
            if (count > 0){
                return ServerResponse.createBySuccessMessage("密码修改成功");
            }

        }else {
            return ServerResponse.createByErrorMessage("token错误，请重新获取修改密码的token");
        }
        return ServerResponse.createByErrorMessage("修改密码失败");


    }


}
